import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import vuexPersist from "vuex-persist";
export default new Vuex.Store({
  state: {
    list: [
      {
        id: 1,
        title: "任务一",
      },
      {
        id: 2,
        title: "任务2",
      },
      {
        id: 3,
        title: "任务3",
      },
    ],
    sel: [],
    checked: false,
  },
  mutations: {
    add(state, val) {
      var flag = false;
      state.list.forEach((item) => {
        if (item.title == val) {
          flag = true;
          alert("不能重复添加");
        }
      });

      if (flag == false) {
        state.list.push({ id: new Date().getTime(), title: val });
      }

      //保证id是唯一的
    },
    removeall(state) {
      state.list = state.list.filter((item) => {
        return state.sel.includes(item) == false;
      });
      state.sel = [];
      state.checked = false;
    },

    change(state) {
      if (state.sel.length == state.list.length && state.list.length != 0) {
        state.checked = true;
      } else {
        state.checked = false;
      }
    },
    changeall(state) {
      if (state.checked == true) {
        state.sel = state.list;
      } else {
        state.sel = [];
      }
    },
  },
  actions: {},
  modules: {},
  plugins: [
    new vuexPersist({
      localstorage: window.localStorage,
    }).plugin,
  ],
});
